package com.example.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.demo.mapper.RoleMapper;
import com.example.demo.pojo.BackRole;
import com.example.demo.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    RoleMapper roleMapper;
    @Override
    public List<BackRole> roleList(int id, String name) {
        List<BackRole> backRoles=roleMapper.roleList(id,name);
        return backRoles;
    }

    @Override
    public PageInfo<BackRole> backroleinfo(int page, int size, String id, String name) {
        int a=0;
        if(id.equals("")){
            a=-1;
        }else{
            a=Integer.parseInt(id);
        }
        PageHelper.startPage(page,size);
        List<BackRole> backRoles=roleMapper.roleList(a,name);
        PageInfo<BackRole> rolePageInfo =new PageInfo<>(backRoles);
        return rolePageInfo;
    }

    @Override
    public String updateRole(String name, int seq) {

        String message="";
        List<BackRole> backRoles=roleMapper.roleList(-1,name);
        if(backRoles.size()>=1){
            message="已经存在";
        }else{
            roleMapper.updateRole(name,seq);
            message="成功";
        }
        return JSON.toJSONString(message);

    }

    @Override
    public String roleInsert(String role) {
        String message="";
        List<BackRole> backRoles=roleMapper.roleList(-1,role);
        if(backRoles.size()>=1){
            message="已经存在";
        }else{
            roleMapper.roleInsert(role);
            message="成功";
        }
        return JSON.toJSONString(message);

    }

    @Override
    public String roleDelete(int id) {
        roleMapper.roleDelete(id);
        String message="成功";
        return JSON.toJSONString(message);

    }
}
